package server;

import java.io.IOException;
import java.util.LinkedList;
import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class provides the functionality for search for
 * seeds, hardness zone and so on 
 * @author Spencer Malone
 */
@WebServlet(name = "Search", urlPatterns = {"/Search"})
public class Search extends HttpServlet {
        
        /**
         * This searches for seeds by criteria and then returns them search results to the page.
         * The results are formatted as a table.
         * 
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		           try{
        String criteria = request.getParameter("criteria");
        String searchby = request.getParameter("searchby");
        LinkedList<Seed> result=SeedHandler.findSeed(criteria, searchby);
        
        String ret = "";
        for(Seed s:result){
        	
        	ret += "<tr><td><a href=seed.jsp?id='" + s.getSeedID() + "'>" 
        	+ s.getComName() + "</a></td><td>" + s.getSciName() + "</td><td>" 
			+ s.getHardiness() + "</td><td>" + s.getHarvestStart() + "</td><td>" 
        	+ s.getHarvestEnd() + "</td></tr>";
        }
        request.setAttribute("results", ret);
        RequestDispatcher rd = request.getRequestDispatcher("search.jsp");
        rd.forward(request,response);
        } finally{
            }
            

	}

	/**
	 * Does nothing.
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}